package com.sann.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sann.common.utils.ResultCode;
import com.sann.eduservice.entity.chapter.Chapter;
import com.sann.eduservice.entity.chapter.Video;
import com.sann.eduservice.entity.chapter.vo.ChapterVO;
import com.sann.eduservice.entity.chapter.vo.VideoVO;
import com.sann.eduservice.mapper.ChapterMapper;
import com.sann.eduservice.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sann.eduservice.service.VideoService;
import com.sann.servicebase.handler.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程大纲章节 服务实现类
 * </p>
 *
 * @author zjw
 * @since 2021-04-17
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;

    // 获取课程大纲列表,根据课程id进行查询
    @Override
    public List<ChapterVO> getList(String courseId) {
        // 1 获取章节信息
        QueryWrapper<Chapter> query1 = new QueryWrapper<>();
        query1.eq("course_id", courseId);
        List<Chapter> chapterList = this.list(query1);

        // 2 获取小节信息
        QueryWrapper<Video> query2 = new QueryWrapper<>();
        query2.eq("course_id", courseId);
        List<Video> videoList = this.videoService.list(query2);

        //最终要返回的章节VO数据列表
        List<ChapterVO> tree = new ArrayList<>();

        // 遍历填充章节VO数据
        for (Chapter chapter : chapterList) {
            // 创建章节vo对象
            ChapterVO chapterVO = new ChapterVO();
            BeanUtils.copyProperties(chapter, chapterVO);
            tree.add(chapterVO);

            // 封装章节的小节VO集合对象
            List<VideoVO> videos = new ArrayList<>();
            // 遍历填充章节下包含的小节VO数据
            for (Video video : videoList) {
                //判断：小节里面chapterId和章节里面id是否一样
                if (video.getChapterId().equals(chapter.getId())) {
                    // 创建小节VO
                    VideoVO videoVO = new VideoVO();
                    BeanUtils.copyProperties(video, videoVO);
                    videos.add(videoVO);
                }
            }

            //把小节VO数据填充到章节VO中
            chapterVO.setChildren(videos);
        }
        return tree;
    }

    /**
     * 根据chapterId章节id 查询小节表，如果查询有小节数据，不进行删除
     */
    @Override
    public boolean deleteById(String chapterId) {
        // 查询章节下小节数据
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);
        if (count>0) {
            throw new GuliException(ResultCode.ERROR, "请先删除该章节下的小节数据");
        }else{
            int result = this.baseMapper.deleteById(chapterId);
            return result >0;
        }
    }

    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper();
        wrapper.eq("course_id", courseId);
        this.baseMapper.delete(wrapper);
    }
}
